package com.gome.ocean.common.utils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.github.pagehelper.Page;
import com.google.common.collect.Lists;

public class ResponsePage<T> implements Serializable {

    private static final long serialVersionUID = 8656597559014685635L;
    private long              total;                                  // 总记录数
    private List<T>           list;                                   // 结果集
    private int               pageNum;                                // 第几页
    private int               pageSize;                               // 每页记录数
    private int               pages;                                  // 总页数
    private int               size;                                   // 返回结果集的大小
    private Boolean           success;                                // 是否获取成功
    private String            message;                                // 提示信息
    private boolean           hasAuth;                                // 是否有权限

    public ResponsePage(){
    };

    /**
     * 没有权限
     */
    public ResponsePage setNoAuth() {
        this.hasAuth = false;
        this.message = "您没有权限";
        return this;
    }

    /**
     * 包装Page对象，因为直接返回Page对象， 在JSON处理以及其他情况下会被当成List来处理，而出现一些问题。
     * 
     * @param list page结果
     */
    public ResponsePage(List<T> list){
        if (list instanceof Page) {
            Page<T> page = (Page<T>) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.total = page.getTotal();
            this.pages = page.getPages();
            this.list = new ArrayList<T>(page);
            this.size = page.size();
        }
    }

    public ResponsePage setJustList(List<T> list, String msg) {
        this.total = list.size();
        this.pages = 1;
        this.size = list.size();
        this.list = list;
        this.success = true;
        this.message = msg;
        return this;
    }

    public ResponsePage setSingleObject(T obj, String msg) {
        this.total = 1L;
        this.size = 1;
        this.pages = 1;
        this.success = true;
        List<T> list = Lists.newArrayList();
        list.add(obj);
        this.list = list;
        return this;
    }

    public ResponsePage setErrorMsg(String msg) {
        this.success = false;
        this.message = msg;
        return this;
    }

    public ResponsePage setSuccessMsg(String msg) {
        this.success = true;
        this.message = msg;
        return this;
    }

    public ResponsePage emptyPage(String msg) {
        this.setSuccess(true);
        this.setSize(0);
        this.setPages(0);
        List<T> list = Collections.emptyList();
        this.setList(list);
        this.setMessage(msg);
        return this;
    }

    public ResponsePage setPage(Page<T> page, String msg) {
        this.pageNum = page.getPageNum();
        this.pageSize = page.getPageSize();
        this.total = page.getTotal();
        this.pages = page.getPages();
        this.list = new ArrayList<T>(page);
        this.size = page.size();
        this.message = msg;
        this.success = true;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

}
